﻿using ExpenseSystem.Common;
using ExpenseSystem.Core.CustomerModules;
using ExpenseSystem.Core.DataAddOn;
using ExpenseSystem.Core.OrderModules;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;

namespace ExpenseSystem.Core.PayModules
{
    public class PayUpdCusAndOrdManager: PayManager
    {
        PayManager orgPayManager;
        CustomerManager customerManager = new CustomerBaseManager();
        OrderManager orderManager = new OrderBaseManager();

        public PayUpdCusAndOrdManager(PayManager payManager)
        {
            orgPayManager = payManager;
        }
        private bool UpdateCustomer(long site,long cusId,decimal amount,out string message)
        {
            bool result = false;
            CustomerFetcher customerFetcher = new CustomerFetcher();
            CustomerModel customerInfo = customerFetcher.Get(site, cusId, out message);
            if (customerInfo == null)
            {
                LogHelper.Error(this.GetType().FullName, "无法找到对应的客户信息");
                return false;
            }
            customerInfo.Received += amount;
            result = customerManager.Save(customerInfo, out message);
            if (result)
            {
                LogHelper.Info(this.GetType().FullName, "更新客户累计付款信息成功");
            }
            else
            {
                LogHelper.Error(this.GetType().FullName, string.Format("更新客户累计付款失败:{0}", message));
            }
            return result;
        }
        private bool UpdateOrder(long site,long ordId,decimal amount,out string message)
        {
            if (ordId == 0)
            {
                message = "";
                return true;
            }
            bool result = false;
            OrderFetcher orderFetcher = new OrderFetcher();
            OrderModel orderModel = orderFetcher.Get(site,ordId,  out message);
            if (orderModel == null)
            {
                LogHelper.Error(this.GetType().FullName, "无法找到对应的订单信息");
                return result;
            }
            orderModel.Receipt += amount;
            result = orderManager.Save(orderModel, out message);
            if (result)
            {
                LogHelper.Info(this.GetType().FullName, "更新客户累计付款信息成功");
            }
            else
            {
                LogHelper.Error(this.GetType().FullName, string.Format("更新客户累计付款失败:{0}", message));
            }
            return result;
        }
        public override bool Approved(long id, out string message)
        {
            PayFetcher payFetcher = new PayFetcher();
            PayModel payModel;
            decimal amount = 0;
            if (id == 0)
            {
                message = "传入的参数有误";
                return false;
            }
            else
            {
                payModel = payFetcher.Get(profile.SiteId, id, out message);
                if (payModel == null)
                    return false;
                amount += payModel.Amount;
            }
            bool result = orgPayManager.Approved(id, out message);
            if (result)
            {
                result = UpdateCustomer(profile.SiteId, payModel.CustomerId, amount, out message);
            }
            if (result&&payModel.OrderId>0)
            {
                result = UpdateOrder(profile.SiteId, payModel.CustomerId, amount, out message);
            }
            return result;
        }
        public override bool Revert(long modelid, out string message)
        {
            PayFetcher payFetcher = new PayFetcher();
            PayModel payModel;
            decimal amount = 0;
            if (modelid == 0)
            {
                message = "传入的参数有误";
                return false;
            }
            else
            {
                payModel = payFetcher.Get(profile.SiteId, modelid, out message);
                if (payModel == null)
                    return false;
                amount -= payModel.Amount;
            }
            bool result = orgPayManager.Revert(modelid, out message);
            if (result)
            {
                result = UpdateCustomer(profile.SiteId, payModel.CustomerId, amount, out message);
            }
            if (result&&payModel.OrderId > 0)
            {
                result = UpdateOrder(profile.SiteId, payModel.CustomerId, amount, out message);
            }
            return result;
        }
        public override bool Delete(PayModel Pay, out string message)
        {
            return orgPayManager.Delete(Pay, out message);
        }

        public override bool DeleteById(long[] PayId, out string message)
        {
            return orgPayManager.DeleteById(PayId, out message);
        }

        public override bool Save(PayModel Pay, out string message, params IAddable<PayModel>[] addables)
        {
            bool result = orgPayManager.Save(Pay, out message, addables);
            return result;
        }
    }
}
